#include <allegro.h>

int width = 900;//width of box
int height = 600;//height of box
int radius = 5;//radius of ball

int x = 450;//initial position of ball
int y = 300;//initial position of ball

int tempX;
int tempY;
int dir;
int cursor_x;
int cursor_y;
int cont=0;
int pendiente;

void getMouseInfo(){

     if(mouse_b & 1){

                  cursor_x = mouse_x;
                  cursor_y = mouse_y;
     }
}


void moveBall(pendiente){
  //Save current location of ball.
  tempX = x;
  tempY = y;

  if (cursor_x>x) x=x+1;
  else if(cursor_x<x)x=x-1;
  if (cursor_y>y) y=y+x*pendiente;
  else if(cursor_y<y)y=y-x*pendiente;
  
  //Erase the current image of the ball and draw a new
  // image at the modified location coordinates.
  acquire_screen();
  circlefill (screen,tempX,tempY,radius,makecol(0,0,0));
  circlefill (screen,x,y,radius,makecol( 128, 255, 0));
  release_screen();
if (cont>5) rest(700/cont);//Delay for five milliseconds
else rest(700/5);//Delay for five milliseconds
  --cont;
}// end moveBall function.

int main(){
  allegro_init();
  install_keyboard();
  install_mouse();
  set_color_depth(32);
  set_gfx_mode(GFX_AUTODETECT_WINDOWED,width,height,0,0);

  //Seed the random number generator and set the initial
  // direction based on a random number.
  srand (time(NULL));
  acquire_screen();
  circlefill (screen,tempX,tempY,radius,makecol(0,0,0));
  circlefill (screen,x,y,radius,makecol( 128, 255, 0));
  release_screen();
 

while(!key[KEY_ESC]){

 show_mouse(screen);
  //Loop until the user presses the Esc key.
  while( !mouse_b && !key[KEY_ESC]){}//end while loop
  
  getMouseInfo();
  cursor_x = mouse_x;
  cursor_y = mouse_y;

  pendiente=(cursor_y-y)/(cursor_x-x);
  if(pendiente<0) pendiente=pendiente*(-1);
  if(mouse_y<y){ dir=1;cont=(y-mouse_y)*2;}
  if(mouse_y>y){ dir=3;cont=(mouse_y-y)*2;}
  show_mouse(NULL) ;
  
  while(!key[KEY_ESC] && cont>0) moveBall(pendiente);

}
  return 0;
}//end main
END_OF_MAIN();

